// File Name: flash_ctrl.h
// Author: kukukukuku~
// Date: 2024-07-12

#ifndef __FLASH_CTRL_H
#define __FLASH_CTRL_H

#include "at32f421.h"

//扇区大小
#define FLASH_SECTORS_SIZE                      (0x400)
//扇区63起始地址
#define FLASH_ADDR_SECTORS_63_START             (0x0800FC00)
//扇区63结束地址
#define FLASH_ADDR_SECTORS_63_STOP              (0x0800FFFF)
//数据存储起始地址
#define FLASH_ADDR_DATA_START                   FLASH_ADDR_SECTORS_63_START
//数据存储结束地址
#define FLASH_ADDR_DATA_STOP                    FLASH_ADDR_SECTORS_63_STOP

#define FLASH_DATA_NUM                          (20)
#define FLASH_DATA_KEY_CLICK_BEEP_INDEX         (0)
#define FLASH_DATA_KEY_LONG_BEEP_INDEX          (1)
#define FLASH_DATA_LED1_MODE_INDEX              (2)
#define FLASH_DATA_LED1_BLINK_FREQ_INDEX        (3)
#define FLASH_DATA_LED1_BREATH_STEP_INDEX       (4)
#define FLASH_DATA_LED2_MODE_INDEX              (5)
#define FLASH_DATA_LED2_BLINK_FREQ_INDEX        (6)
#define FLASH_DATA_LED2_BREATH_STEP_INDEX       (7)
#define FLASH_DATA_OLED_DIR_INDEX               (8)
#define FLASH_DATA_OLED_LIGHT_INDEX             (9)
#define FLASH_DATA_STEP_MOTOR_MODE_INDEX        (10)
#define FLASH_DATA_STEP_MOTOR_SPEED_INDEX       (11)
#define FLASH_DATA_WS2812B_CW_MODE_INDEX        (12)
#define FLASH_DATA_WS2812B_CW_R_INDEX           (13)
#define FLASH_DATA_WS2812B_CW_G_INDEX           (14)
#define FLASH_DATA_WS2812B_CW_B_INDEX           (15)
#define FLASH_DATA_WS2812B_CCW_MODE_INDEX       (16)
#define FLASH_DATA_WS2812B_CCW_R_INDEX          (17)
#define FLASH_DATA_WS2812B_CCW_G_INDEX          (18)
#define FLASH_DATA_WS2812B_CCW_B_INDEX          (19)

extern uint16_t flashData[FLASH_DATA_NUM]; //Flash数据

extern uint8_t flashSaveFlag;  //Flash保存结果标志
extern uint8_t flashReadFlag;  //Flash读取结果标志

//擦除扇区
uint8_t flash_erase_sectors(const uint32_t flashSectorsAddrStart);

//写入字节数据
uint8_t flash_program_data_byte(const uint32_t flashAddr, uint8_t data);

//写入半字数据
uint8_t flash_program_data_halfword(const uint32_t flashAddr, uint16_t data);

//写入字数据
uint8_t flash_program_data_word(const uint32_t flashAddr, uint32_t data);

#endif //__FLASH_CTRL_H
